package com.example.esjdsearch.utils;

import com.example.esjdsearch.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bothin
 * At 2020/4/11 10:42
 * Description: 根据keyword抓取京东的数据，进行仿京东搜索
 */
public class HtmlParseUtil {

    private static final String url = "https://search.jd.com/Search";

    /**
     * url样例: https://search.jd.com/Search?keyword=java&enc=utf-8
     * @param keyword
     * @return
     */
    public static List<Content> getJdData(String keyword) throws Exception {
        Document document = Jsoup.parse(new URL(url + "?keyword=" + keyword + "&enc=utf-8"),3000);
        Element goodsListBox = document.getElementById("J_goodsList");
        Elements goods = goodsListBox.getElementsByTag("li");

        List<Content> contentList = new ArrayList<>();

        for (Element good : goods) {
            String img = good.getElementsByTag("img").eq(0).attr("source-data-lazy-img");
            String title = good.getElementsByClass("p-name").text();
            double price = Double.parseDouble(good.select(".p-price i").eq(0).text());

            Content content = Content.builder().img(img).price(price).title(title).build();

            contentList.add(content);
        }

        return contentList;
    }

    public static void main(String[] args) throws Exception {
        //测试
        getJdData("心理学").forEach(System.out::println);
    }
}
